package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Camera;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.methods.Food;
import webjoch.misc.Functions;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;

/**
 * 
 * @author UbserMouse
 *
 */

public class SlidingBlock extends Puzzle{
	Room room;
	boolean solved;
	
	final int[] TILE_1 = {12125, 12133, 12141, 12149}, 
				TILE_2 = {12126, 12134, 12142, 12150}, 
				TILE_3 = {12127, 12135, 12143, 12151};
	final int[] TILE_4 = {12128, 12136, 12144, 12152}, 
				TILE_5 = {12129, 12137, 12145, 12153}, 
				TILE_6 = {12130, 12138, 12146, 12154};
	final int[] TILE_7 = {12131, 12139, 12147, 12155}, 
				TILE_8 = {12132, 12140, 12148, 12156}, 
				TILE_9 = null;
	final int[][] ID_ROW_1 = {TILE_1, TILE_2, TILE_3};
	final int[][] ID_ROW_2 = {TILE_4, TILE_5, TILE_6};
	final int[][] ID_ROW_3 = {TILE_7, TILE_8, TILE_9};
	final int[][][] ID_SPOTS = {ID_ROW_1, ID_ROW_2, ID_ROW_3};
	final int[] BACKGROUNDS = {54321, 54322, 54323};
	
	private int[] SLIDERS = {12125, 12126, 12127, 12128, 12129, 12130, 12131, 12132, 12133, 12134, 12135, 12136, 12137, 12138, 12139, 12140, 12141, 12142, 12143, 12144, 12145, 12146, 12147, 12148, 12149, 12150, 12151, 12152, 12153, 12154, 12155, 12156};
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
		solved = false;
	}

	@Override
	public boolean isValid() {
		Npc[] sliderNpcs = Npcs.getLoaded(new Filter<Npc>(){
			public boolean accept(Npc o){
				return room.getArea().contains(o.getLocation()) && Functions.inArray(SLIDERS, o.getId());
			}
		});
		return sliderNpcs.length > 0;
	}

	@Override
	public Type getType() {
		return Type.SLIDING_BLOCK;
	}

	public Npc getNextSlide(){
		Tile start = Functions.roomLocationToTile(room, new int[] {5, 11});
        if (start == null) return null;
		final int sX = start.getX(), sY = start.getY();
        final Tile[] ROW_1 = {new Tile(sX + 1, sY - 1), new Tile(sX + 3, sY - 1), new Tile(sX + 5, sY - 1)};
        final Tile[] ROW_2 = {new Tile(sX + 1, sY - 3), new Tile(sX + 3, sY - 3), new Tile(sX + 5, sY - 3)};
        final Tile[] ROW_3 = {new Tile(sX + 1, sY - 5), new Tile(sX + 3, sY - 5), new Tile(sX + 5, sY - 5)};
        final Tile[][] SLIDE_SPOTS = {ROW_1, ROW_2, ROW_3};
        while (getTopAt(ROW_3[2]) != null) {
            for (int I = 0; I < SLIDE_SPOTS.length; I++) {
                for (int J = 0; J < SLIDE_SPOTS[I].length; J++) {
                    Tile t = SLIDE_SPOTS[I][J];
                    if (getTopAt(t) == null) {
                        return room.getNpc(ID_SPOTS[I][J]);
                    }
                }
            }
        }
        return null;
	}
	
	@Override
	public int loop() {
        Npc nextSlide = getNextSlide();
        Camera.turnTo(nextSlide);
        Camera.setPitch(false);
        nextSlide.getLocation().hover();
        if (nextSlide.interact("move")) {
        	Task.sleep(1000,1500);
            while (nextSlide.isMoving()){
            	if (Food.getTotalLifePoints() < 60) Food.eat();
                Camera.setPitch(true);
            }
        }
		return 1;
	}

	@Override
	public boolean isFinished(){
		return getNextSlide() == null;
	}

	public static Npc getTopAt(final Tile tile) {
        return Npcs.getNearest(new Filter<Npc>()
        {
            public boolean accept(Npc npc) {
                return npc.getLocation().equals(tile);
            }
        });
    }

	public void messageReceived(MessageEvent e) {
		if (e.getMessage().contains("hear a click")) solved = true;
	}

	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}
}
